package com.zlkj.pro.core.util;

import com.zlkj.pro.core.enums.CodeEnum;

/**
 * 枚举工具类
 *
 * @author lb
 * @version v1.0
 * @date 2021/9/13 12:59
 */
public class EnumUtil {

    /**
     * 根据code获取枚举
     *
     * @author lb
     * @date 2021/9/13 12:59
     */
    public static <T extends CodeEnum> T codeToEnum(Integer code, Class<T> enumClass) {
        //通过反射取出Enum所有常量的属性值
        if (null != code) {
            for (T each : enumClass.getEnumConstants()) {
                //利用code进行循环比较，获取对应的枚举
                if (code.equals(each.getCode())) {
                    return each;
                }
            }
        }
        return null;
    }

    /**
     * 编码转化成中文含义
     *
     * @author lb
     * @date 2021/9/13 12:59
     */
    public static <T extends CodeEnum> String codeToMessage(Integer code, Class<T> enumClass) {
        if (null != code) {
            for (T e : enumClass.getEnumConstants()) {
                if (code.equals(e.getCode())) {
                    return e.getMessage();
                }
            }
        }
        return "未知";
    }

}
